//
//  SEGHouseholdPraiseViewController.m
//  YYKitDemo
//
//  Created by 刘山 on 2017/11/2.
//  Copyright © 2017年 Sam. All rights reserved.
//

#import "SEGHouseholdPraiseViewController.h"
#import "SEGTimeView.h"
#import "SEGFiveStarCell.h"
#import "SEGDigitalManager.h"
// ViewModel
#import <SEGViewModel/SEGHouseholdPraiseViewModel.h>

@interface SEGHouseholdPraiseViewController ()<UITableViewDelegate, UITableViewDataSource,SEGFiveStarCellDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *layouts;
@property (nonatomic, strong) SEGHouseholdPraiseViewModel *viewModel;

@end

@implementation SEGHouseholdPraiseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kColor_bg;
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(0);
    }];
    
    self.title = @"住户好评";
    [self showCustomNavigationBackButton];
    _layouts = [NSMutableArray array];
    
    // add refresh
    WeakSelf;
    [self.tableView addWindmillWithHeaderRefreshBlock:^{
        [weakSelf loadData:YES];
    } footerRefreshBlock:^{
        [weakSelf loadData:NO];
    }];
    [self.tableView beginHeaderRefreshing];
}

- (void)loadData:(BOOL)isRefresh {
    WeakSelf;
    [SVProgressHUD show];
    RACSignal *signal = [self.viewModel refreshRequestSignal:isRefresh];
    [signal subscribeNext:^(NSArray<SEGNewQuizModel *> * _Nullable quizArray) {
        [SVProgressHUD dismiss];
        [weakSelf.tableView stopAnimatingWindmill];
        [weakSelf.layouts removeAllObjects];
        // model转换为layout
        [quizArray enumerateObjectsUsingBlock:^(SEGNewQuizModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            SEGFiveStarCellModel *cellDisplayModel = [SEGFiveStarCellModel new];
            cellDisplayModel.content = obj.quizContent;
            cellDisplayModel.time = obj.createTime;
            cellDisplayModel.link = obj.linkUrl;
            SEGFiveStarCellLayout *layout = [[SEGFiveStarCellLayout alloc]initWithData:cellDisplayModel];
            [weakSelf.layouts addObject:layout];
        }];
        [weakSelf.tableView reloadData];
        
    } error:^(NSError * _Nullable error) {
        [SVProgressHUD dismiss];
        [weakSelf.tableView stopAnimatingWindmill];
        [SEGAlertView showTmepMessageWithMessage:error.localizedDescription];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _layouts.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *cellID = @"cell";
    SEGFiveStarCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[SEGFiveStarCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
     cell.delegate = self;
    [cell setLayout:_layouts[indexPath.row]];
    return cell;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return ((SEGFiveStarCellLayout *)_layouts[indexPath.row]).height;
}

- (BOOL)tableView:(UITableView *)tableView shouldHighlightRowAtIndexPath:(NSIndexPath *)indexPath {
    return NO;
}

// 设置分割线
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    UIEdgeInsets insets = UIEdgeInsetsZero;//屏幕等宽分割线
    if ([cell respondsToSelector:@selector(setSeparatorInset:)]) {
        [cell setSeparatorInset:insets];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:insets];
    }
    if ([cell respondsToSelector:@selector(setPreservesSuperviewLayoutMargins:)]) {
        [cell setPreservesSuperviewLayoutMargins:NO];
    }
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:insets];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:insets];
    }
}

#pragma mark - SEGFiveStarCellDelegate
-(void)seg_openLinkUrl:(SEGFiveStarCellModel *)data{
    NSDictionary* dic = @{
                          @"title" : @"奖状",
                          @"href" : data.link,
                          @"typeId" : @"0"
                          };
    [[SEGUhomeMediator sharedInstance]seg_pushToViewControllerWithType:dic];
}

#pragma mark - Getter

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [UITableView new];
        _tableView.frame = self.view.bounds;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.scrollIndicatorInsets = _tableView.contentInset;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.backgroundView.backgroundColor = [UIColor clearColor];
        _tableView.tableFooterView = [[UIView alloc] init];
    }
    return  _tableView;
}

- (SEGHouseholdPraiseViewModel *)viewModel {
    if (!_viewModel) {
        _viewModel = [[SEGHouseholdPraiseViewModel alloc] init];
    }
    return _viewModel;
}

@end
